package com.snippeteer.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Configurable
public class User{
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Integer id;
    @Column(name="username")
    private String username;
    @Column(name="password")
    private String password;
	public Integer getId() {
		return this.id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	} 
	
	public static User findById(final Integer id) {
	    EntityManager em = User.entityManager();
	    TypedQuery<User> q = em.createQuery("SELECT User FROM User AS user WHERE user.id = :id", User.class);
	    q.setParameter("id", id);
	    return q.getResultList().get(0);
	}
	
	@PersistenceContext
    private transient EntityManager entityManager;
	
	/**
     * Remove the entity from the database.
     */
    @Transactional
    public void remove() {
        if (this.entityManager == null) {
            this.entityManager = entityManager();
        }
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
        	User attached = User.findById(this.id);
            this.entityManager.remove(attached);
        }
    }
	
	/**
     * Save the entity (on create).
     */
    @Transactional
    public void persist() {
        if (this.entityManager == null) {
            this.entityManager = entityManager();
        }
        this.entityManager.persist(this);
    }

    /**
     * Write the entity to the database.
     */
    @Transactional
    public void flush() {
        if (this.entityManager == null) {
            this.entityManager = entityManager();
        }
        this.entityManager.flush();
    }

    /**
     * Merge the entity with other cached versions.
     * @return the merged entity
     */
    @Transactional
    public User merge() {
        if (this.entityManager == null) {
            this.entityManager = entityManager();
        }
        User merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    /**
     * Get the Entity Manager for the entity.
     * @return the EntityManager
     */
    public static final EntityManager entityManager() {
        EntityManager em = new User().entityManager;
        if (em == null) {
            throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        }
        return em;
    }
  
}
